using System;

using Derm.Render;

namespace Derm.Scene.Mesh
{
	/// <summary>
	/// A simple plane.
	/// </summary>
	public class MeshGizmo : Mesh
	{
		#region Constructors

		/// <summary>
		/// 
		/// </summary>
		/// <param name="size"></param>
		public MeshGizmo(float size)
		{
			// Vertex array
			VertexArrayObject gizmoArray = new VertexArrayObject();
			ArrayBufferObject<Vertex3f> gizmoPosition = new ArrayBufferObject<Vertex3f>(BufferObject.Hint.StaticCpuDraw);
			ArrayBufferObject<ColorRGBAF> gizmoColor = new ArrayBufferObject<ColorRGBAF>(BufferObject.Hint.StaticCpuDraw);

			gizmoPosition.Define(6);
			gizmoPosition[0] = Vertex3f.Zero; gizmoPosition[1] = Vertex3f.UnitX * size;
			gizmoPosition[2] = Vertex3f.Zero; gizmoPosition[3] = Vertex3f.UnitY * size;
			gizmoPosition[4] = Vertex3f.Zero; gizmoPosition[5] = Vertex3f.UnitZ * size;

			gizmoColor.Define(6);
			gizmoColor[0] = gizmoColor[1] = ColorRGBAF.RedColor;
			gizmoColor[2] = gizmoColor[3] = ColorRGBAF.GreenColor;
			gizmoColor[4] = gizmoColor[5] = ColorRGBAF.BlueColor;

			gizmoArray.SetArray(VertexArraySemantic.Position, gizmoPosition);
			gizmoArray.SetArray(VertexArraySemantic.Color, gizmoColor);
			gizmoArray.SetElementArray(Primitive.Line);

			VertexArrays.AddVertexArray(gizmoArray);

			// Shader program
			ShaderCompilerContext compilerContext = new ShaderCompilerContext();
			compilerContext.DefineSymbol("DS_COLOR_PER_VERTEX");
			RenderProgram = ShaderLibrary.CreateShaderProgram(compilerContext, "StandardShader");
		}

		#endregion
	}

	/// <summary>
	/// 
	/// </summary>
	public class MeshSphere : Mesh
	{
		#region Constructors

		public MeshSphere(float radius, uint subdivs, uint slices)
		{
			// Vertex array
			VertexArrayObject gizmoArray = new VertexArrayObject();
			ArrayBufferObject<Vertex3f> gizmoPosition;
			uint gizmoPositionIndex = 0;

			gizmoPosition = new ArrayBufferObject<Vertex3f>(BufferObject.Hint.StaticCpuDraw);
			gizmoPosition.Define(subdivs * (slices - 1));

			float deltaPhi = 180.0f / slices, deltaRho = 360.0f / subdivs;
			
			for (float phi = -90.0f + deltaPhi; phi < -90.0f - deltaPhi; phi += deltaPhi) {
				float y = (float)Math.Sin(phi / 180.0f * Math.PI);

				for (float rho = 0.0f; rho < 360.0f; rho += deltaRho) {
					float x = (float)Math.Cos(rho / 180.0f * Math.PI);
					float z = (float)Math.Sin(rho / 180.0f * Math.PI);

					gizmoPosition[gizmoPositionIndex++] = new Vertex3f(x, y, z);
				}
			}

			gizmoArray.SetArray(VertexArraySemantic.Position, gizmoPosition);
			
			gizmoArray.SetElementArray(Primitive.Point);
		}

		#endregion
	}
}
